package com.hifar.test.api.model;

import com.hifar.test.api.constant.EmployeeProcessingErrorCode;
import java.util.ArrayList;
import java.util.List;

/**
 * 员工数据处理结果
 * 包含详细的处理错误信息
 */
public class EmployeeProcessingResult {
    
    private boolean success;
    private List<EmployeeProcessingErrorCode.EmployeeProcessingErrorResponse> errors;
    private int totalCount;
    private int successCount;
    private int failureCount;
    private long processingTime;
    private String operation;
    
    public EmployeeProcessingResult() {
        this.success = true;
        this.errors = new ArrayList<>();
        this.totalCount = 0;
        this.successCount = 0;
        this.failureCount = 0;
        this.processingTime = 0;
    }
    
    public EmployeeProcessingResult(boolean success) {
        this();
        this.success = success;
    }
    
    /**
     * 添加处理错误
     */
    public void addError(EmployeeProcessingErrorCode errorCode) {
        this.success = false;
        this.errors.add(errorCode.createErrorResponse());
        this.failureCount++;
    }
    
    /**
     * 添加处理错误（带操作信息）
     */
    public void addError(EmployeeProcessingErrorCode errorCode, String operation, String dataId) {
        this.success = false;
        this.errors.add(new EmployeeProcessingErrorCode.EmployeeProcessingErrorResponse(errorCode, operation, dataId));
        this.failureCount++;
    }
    
    /**
     * 添加处理错误（带异常信息）
     */
    public void addError(EmployeeProcessingErrorCode errorCode, String operation, String dataId, Exception exception) {
        this.success = false;
        EmployeeProcessingErrorCode.EmployeeProcessingErrorResponse error = 
            new EmployeeProcessingErrorCode.EmployeeProcessingErrorResponse(errorCode, operation, dataId);
        if (exception != null) {
            error.setStackTrace(getStackTrace(exception));
        }
        this.errors.add(error);
        this.failureCount++;
    }
    
    /**
     * 标记处理成功
     */
    public void markSuccess() {
        this.successCount++;
    }
    
    /**
     * 设置总数
     */
    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }
    
    /**
     * 设置处理时间
     */
    public void setProcessingTime(long processingTime) {
        this.processingTime = processingTime;
    }
    
    /**
     * 设置操作类型
     */
    public void setOperation(String operation) {
        this.operation = operation;
    }
    
    /**
     * 检查是否有错误
     */
    public boolean hasErrors() {
        return !errors.isEmpty();
    }
    
    /**
     * 获取错误数量
     */
    public int getErrorCount() {
        return errors.size();
    }
    
    /**
     * 获取处理成功率
     */
    public double getSuccessRate() {
        if (totalCount == 0) {
            return 0.0;
        }
        return (double) successCount / totalCount * 100;
    }
    
    /**
     * 获取平均处理时间
     */
    public double getAverageProcessingTime() {
        if (totalCount == 0) {
            return 0.0;
        }
        return (double) processingTime / totalCount;
    }
    
    /**
     * 获取异常堆栈信息
     */
    private String getStackTrace(Exception exception) {
        if (exception == null) {
            return null;
        }
        java.io.StringWriter sw = new java.io.StringWriter();
        java.io.PrintWriter pw = new java.io.PrintWriter(sw);
        exception.printStackTrace(pw);
        return sw.toString();
    }
    
    // Getters and Setters
    public boolean isSuccess() {
        return success;
    }
    
    public void setSuccess(boolean success) {
        this.success = success;
    }
    
    public List<EmployeeProcessingErrorCode.EmployeeProcessingErrorResponse> getErrors() {
        return errors;
    }
    
    public void setErrors(List<EmployeeProcessingErrorCode.EmployeeProcessingErrorResponse> errors) {
        this.errors = errors;
    }
    
    public int getTotalCount() {
        return totalCount;
    }
    
    public int getSuccessCount() {
        return successCount;
    }
    
    public void setSuccessCount(int successCount) {
        this.successCount = successCount;
    }
    
    public int getFailureCount() {
        return failureCount;
    }
    
    public void setFailureCount(int failureCount) {
        this.failureCount = failureCount;
    }
    
    public long getProcessingTime() {
        return processingTime;
    }
    
    public String getOperation() {
        return operation;
    }
    
    @Override
    public String toString() {
        return "EmployeeProcessingResult{" +
                "success=" + success +
                ", errors=" + errors.size() +
                ", totalCount=" + totalCount +
                ", successCount=" + successCount +
                ", failureCount=" + failureCount +
                ", processingTime=" + processingTime + "ms" +
                ", successRate=" + String.format("%.2f", getSuccessRate()) + "%" +
                ", averageTime=" + String.format("%.2f", getAverageProcessingTime()) + "ms" +
                '}';
    }
}
